<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model\image_video;

use app\admin\helper\DBhelper;
use app\admin\helper\LangHelper;
use app\admin\model\BaseM;
use app\admin\model\LogsM;
use think\Session;
use think\Validate;
use think\Config;

class AlbumM extends  BaseM
{
    protected $table = 'tb_album';
    protected $module = '相册管理';

    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model(){
        $rule = [
            'label' => 'chsDash',
            'tag' => 'alpha',
        ];
        $msg = [
            'label.chsDash' => '__album.validata.name.chsDash__',
            'tag.alpha' => '__tag.validata.code.alpha__',
        ];
        $msg = LangHelper::convertLang($msg,'field');
        $validate = new Validate($rule, $msg);
        $result = $validate->check(input('post.'));
        if(!$result){
            return op_msg(0,$validate->getError());
        }
        return op_msg(1,'success');
    }

    /**
     * 前端search控件
     * button vale [ add edit del search export ]
     * @return mixed
     */
    public function __search(){
        $search = [
            'button'=>['type'=>'button',
                'value'=>['icon-picture'=>'ace-pic-multi-upload', 'icon-retweet'=>'ace-multi-choose','icon-remove'=>'ace-multi-del']],
        ];
        return LangHelper::convertLang($search);
    }

    /**
     * @param $tag
     * @return array
     */
    public function getAlbumThumb($tag){
        $map=[
            "tag" => $tag,
            "status" => 1
        ];
        $result=[
            'data' => $this->where($map)->order('updated_at desc')->find(),
            'count' => $this->where($map)->count()
        ];
        return $result;
    }

    /**
     * @param $tag
     * @return \think\Paginator
     */
    public function listAllByTag($tag){
        $data = $this->where('tag',$tag)->order('updated_at desc')
            ->paginate(Config::get('page_info')['album'],false,[
                'type'     => 'bootstrap',
                'query' => [
                    'code' => $tag
                ]
            ]);
        $log = [
            'op_uid' => Session::get('username'),
            'op_module' => $this->module,
            'op_type' => 'search',
            'op_origin' => $this->table,
            'op_data' => $tag,
            'op_ip' => getIP(),
            'op_sql' => $this->getLastSql(),
        ];
        LogsM::add($log);
        pr_log($log,'operate');
        return $data;
    }

    /*
     * 新增
     * */
    public function add($allData){
        if($this->saveAll($allData)){
            $op_name = $allData[0]['tag'].'=>';
            foreach ($allData as $data){
                $op_name .= $data['name'].'#';
            }
            $log = [
                'op_uid' => Session::get('username'),
                'op_module' => $this->module,
                'op_type' => 'add',
                'op_origin' => $this->table,
                'op_data' => json_encode($op_name,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                'op_ip' => getIP(),
                'op_sql' => $this->getLastSql(),
            ];
            LogsM::add($log);
            pr_log($log,'operate');
            return op_success();
        }
        return op_fail();
    }

    /*
     * 修改
     * */
    public function edit($data){
        $data['updated_at']=time();
        $id = input('post.id/d');
        //只修改变化字段
        $origin = $this->getById($id)->toArray();
        $data = DBhelper::getChangeData($data,$origin);
        if (!empty($data)){
            $re = $this->where('id',$id)->update($data);
            $last_sql = Config::get('app_debug')?$this->getLastSql():'';
            if($re){
                //删除替换的图片
                if (!empty($data['ab_url']))
                    unlink($origin['ab_url']);
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => $this->module,
                    'op_type' => 'edit',
                    'op_origin' => $this->table,
                    'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $this->getLastSql(),
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success($last_sql);
            }
            return op_fail($last_sql);
        }
        return op_nochange();
    }

    /*
     * 删除
     * */
    public function del(){
        $ids = input('post.ids/a');
        $op_name = [];
        $tag = "";
        foreach ($ids as $id){
            $data = $this->getById($id)->toArray();
            $tag = $data['tag'];
            array_push($op_name,$data['name']);
            //删除原路径图片
            unlink($data['ab_url']);
        }
        if (!empty($op_name)) {
            $op_name = [$tag=>$op_name];
            $re = $this->destroy($ids);
            $last_sql = Config::get('app_debug')?self::getLastSql():'';
            if ($re){
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => $this->module,
                    'op_type' => 'delete',
                    'op_origin' => $this->table,
                    'op_data' => json_encode($op_name,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $this->getLastSql(),
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success($last_sql);
            }
            return op_fail($last_sql);
        }
        return op_superadmin();
    }

}